from flask import Flask, request
from flask_cors import CORS
from gevent import pywsgi

from app.config.conf import conf
from app.util.responseBody import response_sec, response_fai
import service.service as service


app = Flask(__name__)


# route
@app.route('/test')
def test():
    return 'test'


@app.route('/omnicore/getLatestTransactions', methods=['GET', 'POST'])
def api_get_latest_transactions():
    try:
        block_count = int(request.values.get('blockCount'))
        txs = service.get_latest_txs(block_count)
        return response_sec(txs)
    except Exception as e:
        return response_fai(str(e))


# @app.route('/omnicore/isMaliciousAccount', methods=['GET', 'POST'])
# def api_is_malicious_account():
#     address = request.values.get('address')
#     try:
#         abuses = service.get_malicious_account_abuses(address)
#         data = {"isMalicious": len(abuses) > 0, "abuses": abuses}
#         return response_sec(data)
#     except Exception as e:
#         return response_fai(e)


# @app.route('/omnicore/getTransactionOutputs', methods=['GET', 'POST'])
# def api_get_transaction_outputs():
#     txid = request.values.get('txid')
#     try:
#         outputs = service.get_tx_outputs(txid)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"outputs": outputs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getTransactionInputs', methods=['GET', 'POST'])
# def api_get_transaction_inputs():
#     txid = request.values.get('txid')
#     try:
#         inputs = service.get_tx_inputs(txid)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"inputs": inputs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getInputTransactions', methods=['GET', 'POST'])
# def api_get_input_transactions():
#     txid = request.values.get('txid')
#     try:
#         txs = service.get_input_txs(txid)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getAllTransactionsOfAccount', methods=['GET', 'POST'])
# def api_get_all_transactions_of_account():
#     address = request.values.get('address')
#     try:
#         txs = service.get_all_txs_of_account(address)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getPayerTransactionsOfAccount', methods=['GET', 'POST'])
# def api_get_payer_transactions_of_account():
#     address = request.values.get('address')
#     try:
#         txids = service.get_payer_txs_of_account(address)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txids}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getPayeeTransactionsOfAccount', methods=['GET', 'POST'])
# def api_get_payee_transactions_of_account():
#     address = request.values.get('address')
#     try:
#         txs = service.get_payee_txs_of_account(address)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getUnspentTransactionOutputsOfAccount', methods=['GET', 'POST'])
# def api_get_unspent_transactions_of_account():
#     address = request.values.get('address')
#     try:
#         tx_outputs = service.get_unspent_tx_outputs_of_account(address)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txOutputs": tx_outputs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getRelevantMaliciousAccounts', methods=['GET', 'POST'])
# def api_get_relevant_malicious_accounts():
#     address = request.values.get('address')
#     try:
#         accounts = service.get_relevant_malicious_accounts(address)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"maliciousAccounts": accounts}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getLatestLargeBalanceTransactions', methods=['GET', 'POST'])
# def api_get_latest_large_balance_transactions():
#     try:
#         block_count = int(request.values.get('blockCount'))
#         tx_count = int(request.values.get('transactionCount'))
#         txs = service.get_latest_large_balance_txs(block_count, tx_count)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getCachedLargeBalanceTransactions', methods=['GET', 'POST'])
# def api_get_cached_large_balance_transactions():
#     try:
#         txs = service.cached_large_balance_transactions
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getLatestRiskyTransactions', methods=['GET', 'POST'])
# def api_get_latest_risky_transactions():
#     try:
#         block_count = int(request.values.get('blockCount'))
#         txs = service.get_latest_risky_txs(block_count)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getCachedRiskyTransactions', methods=['GET', 'POST'])
# def api_get_cached_risky_transactions():
#     try:
#         txs = service.cached_risky_transactions
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txs": txs}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/addMaliciousAccount', methods=['GET', 'POST'])
# def api_add_malicious_account():
#     try:
#         address = request.values.get('address')
#         message = request.values.get('message')
#         abuser = request.values.get('abuser')
#         service.add_malicious_account(address, message, abuser)
#         return {
#             "code": 200,
#             "message": "添加成功！",
#             "data": {}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getLatestTransactionCount', methods=['GET', 'POST'])
# def api_get_latest_transaction_count():
#     try:
#         block_count = int(request.values.get('blockCount'))
#         tx_count = service.get_latest_tx_count(block_count)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txCount": tx_count}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getTransactionCount', methods=['GET', 'POST'])
# def api_get_transaction_count():
#     try:
#         start_time = int(request.values.get('startTime'))
#         end_time = int(request.values.get('endTime'))
#         tx_count = service.get_tx_count(start_time, end_time)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txCount": tx_count}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getLatestLargeBalanceTransactionCount', methods=['GET', 'POST'])
# def api_get_latest_large_balance_transaction_count():
#     try:
#         block_count = int(request.values.get('blockCount'))
#         min_balance = float(request.values.get('minBalance'))
#         tx_count = service.get_latest_large_balance_tx_count(block_count, min_balance)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txCount": tx_count}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getLatestActiveAddressCount', methods=['GET', 'POST'])
# def api_get_latest_active_address_count():
#     try:
#         block_count = int(request.values.get('blockCount'))
#         addr_count = service.get_latest_active_addr_count(block_count)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"addressCount": addr_count}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getActiveAddressCountInRecentHours', methods=['GET', 'POST'])
# def api_get_active_address_count_in_recent_hours():
#     try:
#         addr_counts = service.cached_active_address_count_in_recent_hours
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"addressCounts": addr_counts}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getTransactionCountInRecentHours', methods=['GET', 'POST'])
# def api_get_transaction_count_in_recent_hours():
#     try:
#         tx_counts = service.cached_transaction_count_in_recent_hours
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"txCounts": tx_counts}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getActiveAddressCount', methods=['GET', 'POST'])
# def api_get_active_address_count():
#     try:
#         start_time = int(request.values.get('startTime'))
#         end_time = int(request.values.get('endTime'))
#         addr_count = service.get_active_addr_count(start_time, end_time)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"addressCount": addr_count}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getExchangeRates', methods=['GET', 'POST'])
# def api_get_exchange_rates():
#     try:
#         exchange_rates = service.get_exchange_rates()
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"exchangeRates": exchange_rates}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}
#
#
# @app.route('/btc/getInputTransactionTree', methods=['GET', 'POST'])
# def api_get_input_transaction_tree():
#     try:
#         root_txid = request.values.get('txid')
#         depth = int(request.values.get('depth'))
#         risky_only = int(request.values.get('riskyOnly'))
#         assert risky_only == 0 or risky_only == 1
#         tree = service.get_input_tx_tree(root_txid, depth, risky_only == 1)
#         return {
#             "code": 200,
#             "message": "查询成功！",
#             "data": {"tree": tree}
#         }
#     except Exception as e:
#         return {"code": 400, "message": str(e), "data": {}}


if __name__ == '__main__':
    # CORS(app, supports_credentials=True)
    # flask_host: str = conf['flaskHost']
    # flask_port: int = conf['flaskPort']
    # pywsgi.WSGIServer((flask_host, flask_port), app).serve_forever()
    app.run()
